﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using LevelEditor.Objects.Layers;

namespace LevelEditor.Components.Layers
{
   public class LayerItem : Panel
    {
        private Layer layer;
        public Layer myLayer
        {
            get { return layer; }
            set { layer = value; }
        }

        private Label layerName;
        public Label LayerName
        {
            get { return layerName; }
            
        }

        private PictureBox previewImage;
        public PictureBox PreviewImage
        {
            get { return previewImage; }
        }

        private bool isSelected;
        public bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; }
        }

        private int rows;
        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }

        private int cols;
        public int Cols
        {
            get { return cols; }
            set { cols = value; }
        }

        private int tileWidth;
        public int TileWidth
        {
            get { return tileWidth; }
            set { tileWidth = value; }
        }

        private int tileHeight;
        public int TileHeight
        {
            get { return tileHeight; }
            set { tileHeight = value; }
        }

        private PictureBox visibleFlag;
        private Image visibleImage;
        private Image invisibleImage;

        private PictureBox backgroundImage;

        private Color defaultColor;
        private Color selectedColor;
        private LevelEditor editor;

        private Font font;

        private ContextMenu mContextMenu;

        private bool showingGrid;
        public bool ShowingGrid
        {
            get { return showingGrid; }
             set 
            {
                showingGrid = value;
                this.ContextMenu.MenuItems[0].Checked = showingGrid;
                if (showingGrid)
                {
                    this.editor.stage1.DrawLayerGrid(this);
                }
                else this.editor.stage1.HideGrid();
            }
        }

        public LayerItem(LevelEditor editor, string name) : base()
        {
            layer = new Layer(editor);
            this.editor = editor;

            this.font = new Font(FontFamily.GenericSansSerif, 10);

            this.previewImage = new PictureBox();
            previewImage.BackColor = Color.LightGray;
            previewImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            previewImage.Width = 34;
            previewImage.Height = 36;
            previewImage.Enabled = false;

            this.layerName = new Label();
            this.layerName.Width = 50;
            this.layerName.Font = this.font;
            this.layerName.Text = name;
            this.layerName.AutoSize = false;
            this.layerName.TextAlign = ContentAlignment.MiddleCenter;
            this.layerName.BackColor = Color.Transparent;
            this.layerName.Dock = DockStyle.Fill;
            this.layerName.Padding = new Padding(10, 0, 0, 0);
            this.layerName.Enabled = false;

            this.visibleImage = new Bitmap(@"Images\Layer_visible.png");
            this.invisibleImage = new Bitmap(@"Images\Layer_invisible.png");

            this.visibleFlag = new PictureBox();
            this.visibleFlag.Image = new Bitmap(this.visibleImage, new Size(12, 12));
            this.visibleFlag.Width = 12;
            this.visibleFlag.Height = 12;
            this.visibleFlag.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            this.visibleFlag.Location = new Point(100 - visibleFlag.Width/2, 0);
            this.visibleFlag.BackColor = Color.Transparent;
            this.visibleFlag.Click += visibleFlag_Click;

            this.defaultColor = Color.LightGray;
            this.selectedColor = Color.LightSkyBlue;
            this.BackColor = defaultColor;
            this.Width = 110;
            this.Height = 36;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Padding = new Padding(0);
            this.Margin = new Padding(2, 0, 0, 0);
            this.AllowDrop = true;

            backgroundImage = new PictureBox
            {
                Dock = DockStyle.Left,
                SizeMode = PictureBoxSizeMode.StretchImage,
                Height = this.Height,
                Width = this.Width,
            };
            backgroundImage.Image = new Bitmap(@"Images\tiles_chars_items_rooms_button.png");
            this.backgroundImage.Enabled = false;

            this.Controls.Add(visibleFlag);

            this.Controls.Add(previewImage);
            this.backgroundImage.Controls.Add(layerName);
            this.Controls.Add(backgroundImage);
            this.DoubleClick += LayerItem_DoubleClick;

            this.mContextMenu = new ContextMenu(new MenuItem[] { new MenuItem("Show Grid", ShowGridClicked){Checked = false,} });

            this.ContextMenu = mContextMenu;

            this.showingGrid = false;
        }

        void LayerItem_DoubleClick(object sender, EventArgs e)
        {
            NewLayer editLayer = new NewLayer(this.editor, this);

            if (editLayer.ShowDialog() == DialogResult.OK)
            {
                this.rows = editLayer.getRows();
                this.cols = editLayer.getCols();
                this.layerName.Text = editLayer.getLayerName();
                this.tileWidth = editLayer.getTileWidth();
                this.tileHeight = editLayer.getTileHeight();
            }
        }

        void ShowGridClicked(object sender, EventArgs e)
        {
            ShowingGrid = !ShowingGrid;
        }

        void visibleFlag_Click(object sender, EventArgs e)
        {
            ChangeVisibility();
        }

        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.Delete();
            }
            base.OnPreviewKeyDown(e);
        }

        public void ChangeVisibility()
        {
            myLayer.Visible = !myLayer.Visible;

            if (myLayer.Visible)
                visibleFlag.Image = new Bitmap(visibleImage);
            else
                visibleFlag.Image = invisibleImage;

            visibleFlag.Refresh();
            visibleFlag.Update();
        }

        public new void Select()
        {
            isSelected = true;
            this.BackColor = selectedColor;

            this.Focus();
        }

        public void Deselect()
        {
            isSelected = false;
            this.BackColor = defaultColor;
        }

        public void Delete()
        {
            for (int k = this.myLayer.Count - 1; k >= 0; k--)
            {
                editor.stage1.Controls.Remove(this.myLayer[k]);
                this.myLayer.RemoveAt(k);
            }

            this.Parent.Controls.Remove(this);
        }

        public void GeneratePreview()
        {
            previewImage.Controls.Clear();
            if (editor.stage1.Size.Width == 0 || editor.stage1.Size.Height == 0) return;

            for (int k = 0; k < this.myLayer.Count; k++)
            {
                PictureBox picBox = new PictureBox();
                picBox.Image = new Bitmap(this.myLayer[k].Image, Util.Util.SizeToNewScreen(this.myLayer[k].Image.Size, editor.stage1.Size, previewImage.Size)); ;
                picBox.Width = picBox.Image.Width;
                picBox.Height = picBox.Image.Height;
                picBox.Location = Util.Util.LocationToNewScreen(this.myLayer[k].Location, editor.stage1.Size, previewImage.Size);

                previewImage.Controls.Add(picBox);
            }
        }
    }
}
